var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/help/OrgHelp.tsx
var OrgHelp_exports = {};
__export(OrgHelp_exports, {
  DeptHelp: () => DeptHelp,
  OrgHelp: () => OrgHelp
});
module.exports = __toCommonJS(OrgHelp_exports);
var import_react = __toESM(require("react"));
var import_baseComponent = require("../../baseComponent");
var import_functionalComponent = require("../../functionalComponent");
var import_util = require("../../util");
var import_org_tree = require("../org-tree");
var import_common = require("./common");
function flatTreeNodes(filter) {
  const result = [];
  const exec = (nodes) => {
    nodes == null ? void 0 : nodes.forEach(({ parentNode, children, ...node }) => {
      if (filter(node)) {
        result.push(node);
      }
      exec(children);
    });
    return result;
  };
  return exec;
}
function OrgTree({ treeRef, isOnlyDept, ...props }) {
  const convertNode = (0, import_util.useRefCallback)((node) => {
    return { ...node, selectable: !isOnlyDept || node.leafType === "DEPT" };
  });
  const onSearch = (value) => {
    var _a;
    (_a = treeRef.current) == null ? void 0 : _a.getApi().filterTreeNode(value);
  };
  const filterContent = () => /* @__PURE__ */ import_react.default.createElement(import_util.Layout, { direction: "row", style: { padding: 5, alignItems: "center" } }, /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, { style: { marginLeft: 10, textAlign: "right" } }, /* @__PURE__ */ import_react.default.createElement(import_baseComponent.Search, { size: "small", allowClear: true, onSearch, placeholder: "输入关键字检索", style: { width: 160 } })));
  const onClick = async (e, node) => {
    if (isOnlyDept && node.leafType !== "DEPT") {
      await import_functionalComponent.message.warning("请选择部门");
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(import_org_tree.OrgTree, { ref: treeRef, showFilter: filterContent, convertNode, onClick, ...props });
}
var { Option } = import_functionalComponent.Tabs;
var { Header, Footer, MultipleButtons } = import_functionalComponent.BaseHelp;
var tableProps = {
  cache: false,
  headerMenu: false,
  style: { borderStyle: "solid", borderColor: "var(--border-color-split, #f0f0f0)", borderWidth: 1 }
};
var columns = [
  {
    dataIndex: "code",
    title: "组织编码"
  },
  {
    dataIndex: "title",
    title: "组织名称"
  }
];
function HelpContent() {
  const {
    contentParams: { isOnlyDept, getFieldValue, helpTitle, helpId, selectedArray, multiple, footer, treeProps },
    randomKey
  } = (0, import_react.useContext)(import_functionalComponent.HelpContext);
  const treeRef = (0, import_react.useRef)();
  const btnRef = (0, import_react.useRef)();
  const rightTable = (0, import_react.useRef)();
  const [activeKey, setActiveKey] = (0, import_common.useTabKey)(() => ["0", "1", "2"]);
  const getTable = () => {
    if (activeKey === "0") {
      const api = treeRef.current.getApi();
      const getSelectedData = () => (api.getSelectedNodes() || []).map(({ parentNode, children, ...r }) => r);
      return {
        getSelectedData,
        getCheckedData: getSelectedData,
        getRows() {
          return flatTreeNodes((node) => !isOnlyDept || node.leafType === "DEPT")(api.getNodes());
        }
      };
    }
    return import_util.iup.getCmpApi(`${helpId}_${randomKey}_${activeKey}`);
  };
  const getResult = () => {
    if (multiple) {
      const result = rightTable.current.getApi().getRows().map((r) => ({
        value: getFieldValue(r),
        label: getFieldValue(r, "label"),
        origin: { ...r }
      }));
      return result.length > 0 ? result : void 0;
    } else {
      const value = activeKey === "0" ? treeRef.current.getApi().getSelectedNodes()[0] : getTable().getSelectedData()[0];
      if (value) {
        const { parentNode, children, ...node } = value;
        return { value: getFieldValue(node), label: getFieldValue(node, "label"), origin: node };
      }
      return void 0;
    }
  };
  const tabTitle = (t) => /* @__PURE__ */ import_react.default.createElement("span", { style: { padding: "0 8px" } }, t);
  tableProps.checkbox = multiple ? "checked" : false;
  tableProps.rowSelected = !multiple;
  tableProps.rowChecked = multiple;
  const borderStyle = "1px solid var(--border-color-split, #f0f0f0)";
  return /* @__PURE__ */ import_react.default.createElement(import_util.Layout, null, /* @__PURE__ */ import_react.default.createElement(Header, { title: helpTitle }), /* @__PURE__ */ import_react.default.createElement(
    import_util.Layout.Flex,
    {
      direction: "row",
      style: { padding: "0 5px", borderBottom: multiple && activeKey !== "0" ? borderStyle : 0 }
    },
    /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, { flex: 1 }, /* @__PURE__ */ import_react.default.createElement(import_functionalComponent.Tabs, { className: "fit-height", size: "small", activeKey, onChange: setActiveKey }, /* @__PURE__ */ import_react.default.createElement(Option, { tab: tabTitle("所有数据"), key: "0" }, /* @__PURE__ */ import_react.default.createElement(import_util.Layout, { direction: "row", style: { height: "100%", border: borderStyle, borderTop: 0 } }, /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, null, /* @__PURE__ */ import_react.default.createElement(OrgTree, { treeRef, isOnlyDept, multiple, ...treeProps })))))),
    multiple && /* @__PURE__ */ import_react.default.createElement(
      MultipleButtons,
      {
        getRowKey: getFieldValue,
        outRef: btnRef,
        getActiveTable: getTable,
        getResultTable: () => rightTable.current.getApi()
      }
    ),
    multiple && /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, { flex: 1, direction: "column", style: { paddingTop: 7, paddingBottom: activeKey === "0" ? 0 : 32 } }, /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, null, /* @__PURE__ */ import_react.default.createElement(
      import_functionalComponent.Table,
      {
        ...tableProps,
        columns,
        dataSource: selectedArray,
        ref: rightTable,
        onRow: (rowIndex) => ({
          onDoubleClick: () => btnRef.current.toLeft(rowIndex)
        })
      }
    ), /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        id: `${helpId}_${randomKey}_pagination`,
        style: {
          position: "absolute",
          display: activeKey === "0" ? "none" : "block",
          left: 15,
          right: 15,
          paddingLeft: 10
        }
      }
    )))
  ), /* @__PURE__ */ import_react.default.createElement(Footer, { getResult }, footer && /* @__PURE__ */ import_react.default.createElement("span", { children: footer })));
}
function BaseOrgHelp(props) {
  const {
    valueField = "code",
    labelField = "title",
    displayField,
    userCodeField,
    title,
    isOnlyDept = false,
    multiple = false,
    modalProps,
    onBeforeOpen,
    treeRequest,
    beforeRequest: beforeTreeRequest,
    footer,
    params: treeParams,
    ...others
  } = props;
  const helpId = isOnlyDept ? "dept" : "org";
  const otherProps = others;
  otherProps.onBeforeOpen = (0, import_util.useRefCallback)(async () => {
    if (onBeforeOpen) {
      const canOpen = await onBeforeOpen();
      if (canOpen === false) {
        return false;
      }
    }
    return {};
  });
  otherProps.valueField = valueField;
  otherProps.userCodeField = userCodeField;
  otherProps.labelField = labelField || displayField;
  otherProps.contentParams = {
    footer,
    helpId,
    multiple,
    valueField,
    treeProps: {
      params: treeParams,
      beforeRequest: beforeTreeRequest,
      treeRequest
    },
    treeRequest,
    beforeTreeRequest,
    labelField: otherProps.labelField,
    isOnlyDept
  };
  otherProps.contentParams.helpTitle = title || (isOnlyDept ? "部门帮助" : "组织帮助");
  const { width, height } = (0, import_util.getGlobalConfig)().default.helpConfig;
  return /* @__PURE__ */ import_react.default.createElement(
    import_functionalComponent.BaseHelp,
    {
      ...otherProps,
      input: false,
      modal: true,
      helpContent: HelpContent,
      multiple,
      modalProps: { width, height, ...modalProps }
    }
  );
}
var OrgHelp = (0, import_util.compHoc)((props) => {
  return /* @__PURE__ */ import_react.default.createElement(BaseOrgHelp, { ...props, isOnlyDept: false });
}, "OrgHelp");
var DeptHelp = (0, import_util.compHoc)((props) => {
  const params = (0, import_react.useMemo)(() => {
    return { ...props.params, containDept: true };
  }, [props.params]);
  return /* @__PURE__ */ import_react.default.createElement(BaseOrgHelp, { ...props, params, isOnlyDept: true });
}, "DeptHelp");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DeptHelp,
  OrgHelp
});
